﻿using System;
using System.Xml;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Doodads
{

    public partial class Menu : DoodadBaseClass
    {

        new protected void Page_Load(object sender, EventArgs e)
        {

            base.Page_Load(sender, e);

            if (IsPostBack)
                return;

            int startPageID;
            String cssClass = "";
            switch (GetPropertyValue("MenuType"))
            {
                case "sideMenu":
                    startPageID = ParentPage.Website.LeftMenuPageID.Value;
                    cssClass = "sideMenu";
                    break;
                case "headerMenu":
                    startPageID = ParentPage.Website.HeaderMenuPageID.Value;
                    cssClass = "headerMenu horizontalMenu";
                    break;
                case "veryTopMenu":
                    startPageID = ParentPage.Website.TopMenuPageID.Value;
                    cssClass = "veryTopMenu horizontalMenu";
                    break;
                case "footerMenu":
                    startPageID = ParentPage.Website.FooterMenuPageID.Value;
                    cssClass = "footerMenu";
                    break;
                default:
                    startPageID = ParentPage.Website.HeaderMenuPageID.Value;
                    cssClass = "badMenuType";
                    break;
            }

            
            MenuHTMLLiteral.Text = "";
            XmlNode node = ParentMasterPage.PageListXML.SelectSingleNode("//Page[PageID=" + startPageID + "]");
            MenuHTMLLiteral.Text += "<ul class='" + cssClass + "'>";
            AddNodes(node);
            MenuHTMLLiteral.Text += "</ul>";

        }

        private void AddNodes(XmlNode node)
        {

            XmlNodeList children = node.SelectNodes("Page");

            if (children.Count == 0)
                return;

            PageProperties page;

            foreach (XmlNode childNode in children)
            {

                page = PageProperties.Create(childNode);
                PageProperties.CalculatePublishStatus(ref page, ParentPage.Website.TimeZone.Value);

                if (!page.IsPublished)
                    continue;

                String url = "";

                if (!String.IsNullOrEmpty(page.PageLinkOverrideURL))
                    url = page.PageLinkOverrideURL;
                else
                    url = page.SEO.GetURL(ParentPage);

                String css = "";
                if (page.PageID == ParentPage.PageID)
                    css = " active";

                String target = "";
                if (page.PageLinkOpenInNewWindow.Value)
                    target = " target='_blank'";
                
                if (childNode.SelectNodes("Page").Count > 0)
                {
                    MenuHTMLLiteral.Text += "<li class='" + page.SEO.MenuLinkCustomClass + "'><a class='fly" + css + "' href='" + url + "' title='" + page.SEO.MenuLinkTitle + "'" + target + ">" + page.SEO.MenuLinkText + "</a>";
                    MenuHTMLLiteral.Text += "<ul>";
                    AddNodes(childNode);
                    MenuHTMLLiteral.Text += "</ul>";
                }else
                    MenuHTMLLiteral.Text += "<li class='" + page.SEO.MenuLinkCustomClass + "'><a class='" + css + "' href='" + url + "' title='" + page.SEO.MenuLinkTitle + "'" + target + ">" + page.SEO.MenuLinkText + "</a>";

                MenuHTMLLiteral.Text += "</li>";

            }

        }

    }
}
